
/**
   * 秒转分钟小时格式
   */
export function getMint (value) {
  let theTime = value// 秒
  let middle = 0// 分
  let hour = 0// 小时
  if (theTime > 59) {
    middle = parseInt(theTime / 60)
    theTime = parseInt(theTime % 60)
  }
  if (middle > 59) {
    hour = parseInt(middle / 60)
    middle = parseInt(middle % 60)
  }
  theTime < 10 ? theTime = '0' + theTime : theTime = theTime
  middle < 10 ? middle = '0' + middle : middle = middle
  hour < 10 ? hour = '0' + hour : hour = hour
  return hour + ':' + middle + ':' + theTime
}
/**
  * 字符串转标准时间
  */

export const insertStr = (soure, start, newStr) => {
  return soure.slice(0, start) + newStr + soure.slice(start)
}
export function changeDateFormat (time, format = 'MM月DD日 hh:mm') {
  if (!time) return ''
  var datetime = new Date(time)
  var o = {
    'Y+': datetime.getFullYear(),
    'M+': datetime.getMonth() + 1,
    'D+': datetime.getDate(),
    'h+': datetime.getHours(),
    'm+': datetime.getMinutes(),
    's+': datetime.getSeconds(),
    'q+': Math.floor((datetime.getMonth() + 3) / 3),
    S: datetime.getMilliseconds()
  }
  if (/(Y+)/.test(format)) format = format.replace(RegExp.$1, (datetime.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return format
}
export function changeDateFormat2 (time, format = 'YYYY年MM月DD日') {
  if (!time) return ''
  var datetime = new Date(time)

  var o = {
    'Y+': datetime.getFullYear(),
    'M+': datetime.getMonth() + 1,
    'D+': datetime.getDate(),
    'h+': datetime.getHours(),
    'm+': datetime.getMinutes(),
    's+': datetime.getSeconds(),
    'q+': Math.floor((datetime.getMonth() + 3) / 3),
    S: datetime.getMilliseconds()
  }
  if (/(Y+)/.test(format)) format = format.replace(RegExp.$1, (datetime.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return format
}
// 倒计时
export function changeDateFormat3 (time) {
  if (!time) return ''
  // var dateee = new Date(date).toJSON();return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
  var datetime = new Date(time).getTime()
  var datetimeNew = new Date().getTime()
  // console.log(datetime,datetimeNew);
  if (datetime - datetimeNew > 0) {
    const date3 = Math.floor((datetime - datetimeNew))
    // 计算出相差天数
    const days = Math.floor(date3 / (24 * 3600 * 1000))
    // 计算出小时数
    const leave1 = date3 % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数
    const hours = Math.floor(leave1 / (3600 * 1000))
    // 计算相差分钟数
    const leave2 = leave1 % (3600 * 1000) // 计算小时数后剩余的毫秒数
    const minutes = Math.floor(leave2 / (60 * 1000))
    // 计算相差秒数
    const leave3 = leave2 % (60 * 1000) // 计算分钟数后剩余的毫秒数
    const seconds = Math.round(leave3 / 1000)
    return days + '天' + hours + '小时' + minutes + '分钟'
  } else {
    return '倒计时已结束'
  }
}
export function changeDateFormat4 (time, format = 'YYYY-MM-DD') {
  if (!time) return ''
  var datetime = new Date(time)

  var o = {
    'Y+': datetime.getFullYear(),
    'M+': datetime.getMonth() + 1,
    'D+': datetime.getDate(),
    'h+': datetime.getHours(),
    'm+': datetime.getMinutes(),
    's+': datetime.getSeconds(),
    'q+': Math.floor((datetime.getMonth() + 3) / 3),
    S: datetime.getMilliseconds()
  }
  if (/(Y+)/.test(format)) format = format.replace(RegExp.$1, (datetime.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return format
}
export function changeDateFormat5 (time) {
  if (!time) return ''
  var datetime = new Date(time).getTime() + 1000 * 60 * 60 * 24 * 30
  var datetime2 = new Date(datetime)
  var year = datetime2.getFullYear()
  var month = datetime2.getMonth() + 1
  var day = datetime2.getDate()
  return year + '-' + month + '-' + day
}
export function changeDateFormat6 (time, format = 'YYYY年MM月DD日 hh:mm:ss') {
  if (!time) return ''
  var datetime = new Date(time)

  var o = {
    'Y+': datetime.getFullYear(),
    'M+': datetime.getMonth() + 1,
    'D+': datetime.getDate(),
    'h+': datetime.getHours(),
    'm+': datetime.getMinutes(),
    's+': datetime.getSeconds(),
    'q+': Math.floor((datetime.getMonth() + 3) / 3),
    S: datetime.getMilliseconds()
  }
  if (/(Y+)/.test(format)) format = format.replace(RegExp.$1, (datetime.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return format
}
export function changeDateFormat7 (time, format = 'YYYY-MM-DD hh:mm:ss') {
  if (!time) return ''
  var datetime = new Date(time)

  var o = {
    'Y+': datetime.getFullYear(),
    'M+': datetime.getMonth() + 1,
    'D+': datetime.getDate(),
    'h+': datetime.getHours(),
    'm+': datetime.getMinutes(),
    's+': datetime.getSeconds(),
    'q+': Math.floor((datetime.getMonth() + 3) / 3),
    S: datetime.getMilliseconds()
  }
  if (/(Y+)/.test(format)) format = format.replace(RegExp.$1, (datetime.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return format
}
export function changeDateFormat8 (time, format = 'hh:mm') {
  if (!time) return ''
  var datetime = new Date(time)

  var o = {
    'Y+': datetime.getFullYear(),
    'M+': datetime.getMonth() + 1,
    'D+': datetime.getDate(),
    'h+': datetime.getHours(),
    'm+': datetime.getMinutes(),
    's+': datetime.getSeconds(),
    'q+': Math.floor((datetime.getMonth() + 3) / 3),
    S: datetime.getMilliseconds()
  }
  if (/(Y+)/.test(format)) format = format.replace(RegExp.$1, (datetime.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return format
}
export function changeDateFormat9 (time, format = 'MM.DD hh:mm') {
  if (!time) return ''
  var datetime = new Date(time)
  var o = {
    'Y+': datetime.getFullYear(),
    'M+': datetime.getMonth() + 1,
    'D+': datetime.getDate(),
    'h+': datetime.getHours(),
    'm+': datetime.getMinutes(),
    's+': datetime.getSeconds(),
    'q+': Math.floor((datetime.getMonth() + 3) / 3),
    S: datetime.getMilliseconds()
  }
  if (/(Y+)/.test(format)) format = format.replace(RegExp.$1, (datetime.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return format
}
export function propYear (data, type) {
  const date = new Date(data)
  var myyear = date.getFullYear()
  var mymonth = date.getMonth() + 1
  var myweekday = date.getDate()

  if (mymonth < 10) {
    mymonth = '0' + mymonth
  }
  if (myweekday < 10) {
    myweekday = '0' + myweekday
  }
  if (type) {
    return myyear + '年' + mymonth + '月' + myweekday + '日'
  } else {
    return myyear + '-' + mymonth + '-' + myweekday
  }
}
export function getNowMonthLast (time) {
  const date = new Date(time)
  const endYear = date.getFullYear()
  const endMonth = date.getMonth() + 1
  const enddate = new Date(endYear, endMonth, 0)
  return enddate
}
// 获取月份
export function getMount (time) {
  const date = new Date(time)
  var mymonth = date.getMonth() + 1
  if (mymonth < 10) {
    mymonth = '0' + mymonth
  }
  return mymonth
}
// 年月日转年月
export function changeYear (data, type) {
  const date = new Date(data)
  var myyear = date.getFullYear()
  var mymonth = date.getMonth() + 1
  var myweekday = date.getDate()

  if (mymonth < 10) {
    mymonth = '0' + mymonth
  }
  if (myweekday < 10) {
    myweekday = '0' + myweekday
  }
  if (type && type === '日') {
    return myweekday
  } else if (type && type === '月') {
    return mymonth + '月'
  } else {
    return myyear + '年' + mymonth + '月'
  }
}
export function getUserIP (onNewIP) {
  //  onNewIp - your listener function for new IPs
  // compatibility for firefox and chrome
  var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection
  var pc = new myPeerConnection({
    iceServers: []
  })
  var noop = function () {}
  var localIPs = {}
  var ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g
  var key

  function iterateIP (ip) {
    if (!localIPs[ip]) onNewIP(ip)
    localIPs[ip] = true
  }

  // create a bogus data channel
  pc.createDataChannel('')

  // create offer and set local description
  pc.createOffer()
    .then(function (sdp) {
      sdp.sdp.split('\n').forEach(function (line) {
        if (line.indexOf('candidate') < 0) return
        line.match(ipRegex).forEach(iterateIP)
      })

      pc.setLocalDescription(sdp, noop, noop)
    })
    .catch(function (reason) {
      // An error occurred, so handle the failure to connect
    })

  // sten for candidate events
  pc.onicecandidate = function (ice) {
    if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return
    ice.candidate.candidate.match(ipRegex).forEach(iterateIP)
  }
}
export function deepClone (obj) {
  var target = {}
  for (var key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
      if (typeof obj[key] === 'object') {
        target[key] = deepClone(obj[key])
      } else {
        target[key] = obj[key]
      }
    }
  }
  return target
}
/**
 *
 * @param {*} phone
 * @returns 脱敏手机号
 */
export function encryptionPhone (phone) {
  const newPhone = phone.replace(/(?<=\d{3})\d+(?=\d{4})/, '****')
  return newPhone
}
/**
 *
 * @param {*} email
 * @returns 脱敏邮箱
 */
export function encryptionEmail (email) {
  const str = email.split('@')
  const str1 = '*****@' + str[1]
  return str1
}
